<?php
$caption = BUtils::T('lunch_menu_lbl');

$this->pageTitle = Yii::app()->name . ' - ' . $caption;
$last_page = Yii::app()->createUrl('/lunch');
?>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/labs/jquery.galleryview/jquery.timers-1.2.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/labs/jquery.galleryview/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/labs/jquery.galleryview/jquery.galleryview-3.0-20120515.js"></script>
<link type="text/css" rel="stylesheet" href="<?php echo Yii::app()->baseUrl; ?>/js/labs/jquery.galleryview/css/jquery.galleryview-3.0-dev.css" />

<div class="container prepend-top">
	<?php echo CHtml::button('< ' . BUtils::T('back_btn'), array('class'=>'button', 'onclick'=>"location.href='{$last_page}'")); ?>
</div>

<div class="container prepend-top append-bottom">
	
	<div class="span-11">
		<div class="box_white">
		<div class="biz-form">
			<div class="margin">
				<h3><?php echo $caption; ?></h3>
				<div class="tips"><?php echo BUtils::T('lunch_menu_eg'); ?></div>
			</div>

<div class="fieldswrap">
	<div class="fields">
		<div class="block">
			<div class="eg"><?php echo BUtils::T('upload_menu_picutre_eg'); ?></div>
			<div class="ipt">
<?php $this->widget('ext.EAjaxUpload.EAjaxUpload', array(
	'id' => 'uploadFile',
	'postParams' => array('prefix' => 'menu_' . Yii::app()->user->id . '_'),
	'config' => array(
		'buttonName' => BUtils::T('upload_menu_picutre_btn'),
		'action' => Yii::app()->createUrl('/merchant/uploadMenu'),
		'allowedExtensions'=>array("jpg","jpeg","gif","png"),
		'sizeLimit'=>8*1024*1024,
		'onComplete' => "js:function(id, fileName, responseJSON){ handleUploaded(responseJSON); }",
	),
)); ?>
			</div>
		</div>
	</div>
	<div class="fields" id="menu_options">
<?php
$current_menu_is_lbl = BUtils::T('current_menu_is_lbl');
$save_changes_btn = BUtils::T('save_changes_btn');
$delete_the_menu_btn = BUtils::T('delete_the_menu_btn');
$valid_days_in_week_lbl = BUtils::T('valid_days_in_week_lbl');
$valid_days_in_week_eg = BUtils::T('valid_days_in_week_eg');

$first_menu_id = null;
if (count($menu_rows) > 0) $first_menu_id = $menu_rows[0]['id'];
echo CHtml::hiddenField('menuForm[id]', $first_menu_id);
foreach ($menu_rows as $key => $menu) {
	$show = ($key === 0) ? 'display:block' : 'display:none';
?>
<div class="menu_opt" id="menu_opt_<?php echo $menu['id']; ?>" style="<?php echo $show; ?>">
<?php echo CHtml::form(Yii::app()->createUrl('/merchant/saveMenu'), 'post'); ?>
	<div class="block">
		<div class="lbl"><?php echo CHtml::label($valid_days_in_week_eg, ''); ?></div>
		<div class="span-4">
			<div class="ipt">
				<?php echo CHtml::checkBoxList('menuForm[week_days]', explode(',', $menu['week_days']), BUtils::WeekDayKeys()); ?>
			</div>
		</div>
		<div class="span-4 right last relative">
			<?php echo CHtml::hiddenField("menuForm[id][{$menu['id']}]", $menu['id']); ?>
			<div id="update-loading-<?php echo $menu['id']; ?>" style="position:absolute; bottom:50%; width:128px; text-align:center;"></div>
			<div class="picture"><?php echo CHtml::image($menu['url_picture'], '', array('width'=>'120')); ?></a></div>
			<div class="eg"><?php echo BUtils::T('last_update_eg'); ?>:<br /><span id="update-time-<?php echo $menu['id']; ?>"><?php echo date('Y-m-d H:i:s', empty($menu['uts_update']) ? $menu['uts_insert'] : $menu['uts_update']); ?></span></div>
		</div>
		<div class="spc"></div>
	</div>
	<div class="margin actions">
		<?php echo CHtml::ajaxSubmitButton($save_changes_btn, Yii::app()->createUrl('/merchant/saveMenu', array('id'=>$menu['id'])), array('success'=>"afterSaveMenu",'beforeSend'=>"beforeSaveMenu"), array('class' => 'button button-huge button-confirm')); ?>
		<?php echo CHtml::button($delete_the_menu_btn, array('class' => 'button button-huge button-thin button-compliment', 'onclick'=>"deleteMenu({$menu['id']}, '{$menu['url_picture']}');", 'confirm'=>BUtils::T('delete_btn_confirm'))); ?>
	</div>
<?php echo CHtml::endForm(); ?>
</div>
<?php } ?>
	</div>
</div>			
		</div><!-- end of <div class="biz-form"> -->
		</div>
	</div>
	
	<div class="span-13 right last">
		<div class="box_white">
			<div class="biz-form">
				<div class="margin">
					<h3><?php echo BUtils::T('menu_preview_lbl'); ?></h3>
					<div class="tips"><?php echo (count($menu_rows) > 0) ? BUtils::T('menu_has_items_lbl') : BUtils::T('menu_has_no_item_lbl'); ?></div>
				</div>
				<div class="fieldswrap">
<?php
if (count($menu_rows) > 0) {
	echo '<ul id="menu-gallery">';
	foreach ($menu_rows as $menu) {
		echo '<li>';
		echo CHtml::image($menu['url_picture'], $menu['id'], array('width'=>450, 'title'=>''));
		echo '</li>';
	}
	echo '</ul>';
}
?>
					<div class="clear"></div>
				</div>
			</div>
		</div>
	</div>
	
</div>

<script type="text/javascript">
<?php echo 'var list=' . htmlspecialchars(json_encode($menu_rows), ENT_NOQUOTES) . ';'; ?>
function handleUploaded(respJSON) {
	if (respJSON.error) {
		$('.qq-upload-button').html('<span>'+respJSON.error+'</span>');
	} else {
		window.location.reload();
		//$('#myGallery').prepend('<li><img src="'+respJSON.url_picture+'" width="200" /></li>');
		//$('#myGallery').galleryView(config); //not work
	}
}
function deleteMenu(id, url) {
	$.post('<?php echo Yii::app()->createUrl('/merchant/deleteMenu') ?>', {id:id, url:url}, function(data) {
		window.location.reload();
	});
}
function beforeSaveMenu() {
	var id = $('#menuForm_id').val();
	$('#update-loading-'+id).html('<img class="process-img" src="<?php echo Yii::app()->baseUrl; ?>/images/loading-31.gif" width="31" alg="" />').show();
}
function afterSaveMenu(data) {
	var id = $('#menuForm_id').val();
	$('#update-loading-'+id).html('<div style="background:#000;padding:3px 10px;color:#fff;"><?php echo BUtils::T('saved_successfully_lbl'); ?></div>').delay(1000).fadeOut('fast');
	$('#update-time-'+id).html(data);
}
$(function() {
	// Initialize Slides
	$('#menu-gallery').galleryView({
		filmstrip_style: 'showall',
		panel_width: 450,
		panel_height: 636,
		frame_width: 60,
		frame_height: 80,
		frame_gap: 4,
		show_infobar: true,
		pan_images: true,
		panel_animation: 'none',
		show_panel_nav: <?php echo (count($menu_rows) > 1) ? 'true':'false'; ?>,
		filmstrip_position: 'top',
		show_filmstrip: <?php echo (count($menu_rows) > 1) ? 'true':'false'; ?>,
		show_filmstrip_nav: false,
		onSlideChange: function(index) {
			$('#menu_options > .menu_opt').hide();
			$('#menu_opt_' + list[index].id).show();
			$('#menuForm_id').val(list[index].id);
		}
	});
});
</script>